|
|
|
|
|
|
|
Yes and no. I opened the NT user manager and found that I had created a user named Pass, not one named Puzzle. How could this be? |
|
|
|
|
|
|
|
|
I cheated and got caught. |
|
|
|
|
|
|
|
|
You noticed that I passed Unicode strings to the structure by using the StrPtr function with a constant, as follows: |
|
|
|
|
|
|
|
|
ul2.usri2_name = StrPtr("puzzle" & chr$(0)) |
|
|
|
|
|
|
|
|
What happens when you use the StrPtr operator on a constant? It creates a temporary variable containing the data and passes a pointer to the data. But as soon as it returns the pointer, the temporary variable ceases to exist. The next time StrPtr is called, the buffer is loaded with a different string. Frankly, I was lucky it didn't cause a system crash. The structure is holding a pointer, and you simply can't hold pointers to temporary data. I tried to get by without creating separate strings, and I got caught. This is where experience comes in handy. I had never tried using the StrPtr operator on a constant before trying it here, and I wanted to see if it would work. I was actually surprised that it did. Fortunately, I did have experience trying to hold pointers to temporary buffers and had seen similar problems, so as soon as I saw that the account was being created with the wrong name, I guessed that it was somehow related to my use of the StrPtr operator with constants. The final working code uses separate string variables whose addresses remain valid through the life of the function (as long as you don't try to change the data in the string). Here's the final version of the function: |
|
|
|
|
|
|
|
|
Private Sub Command1_Click()
Dim u12 As USER_INFO_2
Dim res As Long
Dim parm_err As Long
Dim username As String
Dim userpass As String
username = "Puzzle" & Chr$(0)
userpass = "pass" & Chr$(0)
ul2.usri2_name = StrPtr(username)
ul2.usri2_password = StrPtr(userpass)
ul2.usri2_priv = USER_PRIV_USER
ul2.usri2_flags = UF_NORMAL_ACCOUNT Or UF_SCRIPT
res = NetUserAdd(0, 2, VarPtr(ul2), parm_err)
Debug.Print "Result: " & res
If res <> 0 Then Debug.Print "Error at index: " & parm_err
End Sub |
|
|
|
|
|